import wx from "weixin-js-sdk";
import { mapState } from "vuex";
import routes from '../../routers'; // 引入路由配置
import store from '../../vuex/store';
import Vue from "vue";

var shareLink = store.state.path+"index.html?";//"http://www.jubenxiong.com/aml/index.html";//window.location.href;
var shareimgUrl = store.state.domainUrl+"wxapi/amxlloge.jpg";
var sharetitle = "爱美雪梨";//"暧昧雪梨";
var shareDesc = "其实你才18岁";
var urlLink = store.state.path+'index.html?';

var tokens = "";

export default{

      testFun:function(){
        console.log("link="+shareLink);
      },
      defultShareMsg:function(){
        shareLink = store.state.domainUrl+'wxapi/GetUserInfo/dropindex.php?type=amlzlogin&userid='+store.state.userid;
        shareimgUrl = store.state.domainUrl+"wxapi/amxlloge.jpg";
        sharetitle = "美容模板";
        shareDesc = "让每个美容院都用上我们的模板";
      },
      updateShareMsg:function(slink,simg,sdesc,stitle){
        //更改分享信息
        if(slink!=''){
            shareLink = slink;
        }

        if(simg!=''){
            shareimgUrl = simg;
        }

        if(sdesc!=''){
            shareDesc = sdesc;
        }

        if(stitle!=''){
            sharetitle = stitle;
        }
        this.getWxData();
      },
      getWxData:function(types=0) {
  
        var url = ''
        urlLink = store.state.path+"index.html?";
        console.log("url="+window.location.href+'urllink='+self.urlLink+"urlLink=="+shareLink+"desc="+shareDesc);
        shareLink = urlLink;
        Vue.prototype.$http
          .post(store.state.domainUrl+"wxapi/share.php", {
            urls: urlLink
          })
          .then(response => {
            let data = response.body;
            var wxob = {};
  
            wxob.timestamp = data.timestamp;
            wxob.nonceStr = data.nonceStr;
            wxob.signature = data.signature;
            wxob.link = urlLink;
            wxob.imgUrl = shareimgUrl;
            wxob.title = sharetitle;
            wxob.desc = shareDesc;
            this.setData(wxob, this);
            if(types == 0){
              //this.$router.replace({ name: "combineHome" });
            }
          })
          .catch(function(response) {
            console.log(response);
          });
      },
      setData:function(obj, selfobj) {
        // console.log("this.shareimgUrl=" + selfobj.shareimgUrl);
        wx.config({
          debug: false,
          appId: store.state.wxappid,
          timestamp: obj.timestamp,
          nonceStr: obj.nonceStr,
          signature: obj.signature,
          jsApiList: [
            // 所有要调用的 API 都要加到这个列表中
            "onMenuShareTimeline",
            "onMenuShareAppMessage",
            "onMenuShareQQ",
            "onMenuShareWeibo",
            "onMenuShareQZone",
            "chooseImage",
            "uploadImage",
            "downloadImage",
            "chooseWXPay"
          ]
        });
        wx.ready(function() {
            console.log("shareimgUrl"+shareimgUrl);
          // 在这里调用 API
          wx.onMenuShareTimeline({
            title: sharetitle, // 分享标题
            
            //desc: shareDesc, // 分享描述
  
            link: shareLink,//selfobj.shareLink, // 分享链接
  
            imgUrl: shareimgUrl, // 分享图标
  
            success: function() {
              // 用户确认分享后执行的回调函数
              //console.log("im ok");
            },
  
            cancel: function() {
              // 用户取消分享后执行的回调函数
              //console.log("im fail");
            }
          });
  
          wx.onMenuShareAppMessage({
            title: sharetitle, // 分享标题
  
            desc: shareDesc, // 分享描述
  
            link: shareLink,//selfobj.shareLink, // 分享链接
  
            imgUrl: shareimgUrl, // 分享图标
  
            type: "", // 分享类型,music、video或link，不填默认为link
  
            dataUrl: "", // 如果type是music或video，则要提供数据链接，默认为空
  
            success: function() {
              // 用户确认分享后执行的回调函数
            },
  
            cancel: function() {
              // 用户取消分享后执行的回调函数
            }
          });
  
          wx.onMenuShareQQ({
            title: sharetitle, // 分享标题
  
            desc: shareDesc, // 分享描述
  
            link: shareLink, // 分享链接
  
            imgUrl: shareimgUrl, // 分享图标
  
            success: function() {
              // 用户确认分享后执行的回调函数
            },
  
            cancel: function() {
              // 用户取消分享后执行的回调函数
            }
          });
  
          //微博
          wx.onMenuShareWeibo({
            title: sharetitle, // 分享标题
  
            desc: shareDesc, // 分享描述
  
            link: shareLink, // 分享链接
  
            imgUrl: shareimgUrl, // 分享图标
  
            success: function() {
              // 用户确认分享后执行的回调函数
            },
  
            cancel: function() {
              // 用户取消分享后执行的回调函数
            }
          });
  
          wx.onMenuShareQZone({
            title: sharetitle, // 分享标题
  
            desc: shareDesc, // 分享描述
  
            link: shareLink, // 分享链接
  
            imgUrl: shareimgUrl, // 分享图标
  
            success: function() {
              // 用户确认分享后执行的回调函数
            },
            cancel: function() {
              // 用户取消分享后执行的回调函数
            }
          });
  
        });
  
        wx.error(function(res) {
          //console.log("res="+res.errMsg);
          // alert("error=" + res.errMsg);
        });
      }
}